import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;



public class PasswordGen {
	private int n_Columns;  //number of Col; gets it from canvas; used to compute password (setPassword)
	private int n_Rows;		//number of rows; gets it from canvas; used to compute password (setPassword)
	private String password;	//holds the password
	private int width, height;	//w,h of canvas; used to compute password (setPassword)
	DoodleCanvas canvas;
	private RunScript rs = new RunScript();
	private String mode = "";
	
	public boolean hasChangedSquare=false;
	
	public  PasswordGen(DoodleCanvas canvas, int width, int height){
		this.canvas=canvas;
		setVars(width, height);

		password="";
	}
	
	public String getPassword(){
        try {
            //System.out.println("MD5 hash of string: " + AeSimpleMD5.MD5(pass));
        	password = AeSimpleMD5.MD5(password);
        } catch (NoSuchAlgorithmException ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace();
        } catch (UnsupportedEncodingException ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace();
        }
        
        
		return password.substring(5, 21);
		
	}
	
	public void setVars(int width, int height){
		this.width=width;
		this.height=height;
	}
	
	
	public void setPassord(int px, int py){
		int pxi, pyi;
		pxi=(px*canvas.n_Columns)/width;
		pyi=(py*canvas.n_Rows)/height;
		if(canvas.mouseButtonDown){
			if(px > this.width) return;
			if(py > this.height) return;
			password=password+((Integer.toString((int) pxi)) + (Integer.toString((int) pyi)));
			canvas.glowCell(pxi,pyi);
		}
		//pen Up
		else password=password+((Integer.toString((int) pxi)) + (Integer.toString((int) pyi)));
		
	}
	
	public void setPassord(int px, int py, int canvasX, int canvasY){	

		int pxi, pyi,canvasXi,canvasYi;
		
		//px and py are defined even when mouse is not over canvas. 
		//these if's avoid getting points out of the canvas.
		if(px > this.width) return;
		if(py > this.height) return;
		
		//pxi, pyi => matrix cell identification when mouse is dragged (actual point)
		//canvasXi, canvasYi => start matrix cell identification when mouse is dragged
		pxi=(px*canvas.n_Columns)/width;
		pyi=(py*canvas.n_Rows)/height;
		canvasXi=(canvasX*canvas.n_Columns)/width;
		canvasYi=(canvasY*canvas.n_Rows)/height;
		hasChangedSquare=false;

		//if canvasPi != pi => the cell has changed. Then, update password.
		if(pxi!=canvasXi || pyi!=canvasYi){			
				password=password+((Integer.toString((int) pxi)) + (Integer.toString((int) pyi)));
				canvas.glowCell(pxi,pyi);//p.x, p.y);
				hasChangedSquare=true;
				//log CHANGE SQUARE
				rs.runScript("../logdata.php?system=" + rs.system + "&user=" + rs.username + "&scheme=passpoints" + "&mode=" + mode
				           + "&event=ChangeSq&data=sq1:"+ pxi+pyi+",sq2:" + canvasXi+canvasYi);
//				System.out.println("../logdata.php?system=" + "systemBruna" + "&user=" + "bruna123" + "&scheme=passpoints" + "&mode=" + "modexngSq"
//				           + "&event=ChangeSq&data=sq1:"+ canvasYi + canvasXi +",sq2:" +pyi+ pxi);
				
		}	
		
		
	}
	
	public void clearPassword(){
		password="";
	}
	
	public void setRunScript(RunScript rs){
		this.rs=rs;
	}
	
	public void setMode(String mode){
		this.mode=mode;
	}
}
